package com.zahb.qadx.webview;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.webkit.JavascriptInterface;

import com.zahb.qadx.MainActivity;
import com.zahb.qadx.base.BaseApplication;
import com.zahb.qadx.ui.activity.LoginActivity;
import com.zahb.qadx.ui.activity.MyCertificateActivity;
import com.zahb.qadx.util.ActivityManager;

/**
 * Created by Zdw on 2020/11/11 20:02
 * Js通信接口
 */
public class MyJavascriptInterface {

    private static final String TAG = MyJavascriptInterface.class.getSimpleName();
    private Context mContext;
    private Callback mCallback;

    MyJavascriptInterface(Context context, Callback callback) {
        this.mContext = context;
        this.mCallback = callback;
    }

    @JavascriptInterface
    public void checkTheCertificatec(String data) {
        Log.e(TAG, "checkTheCertificate data: " + data);
        MyCertificateActivity.actionStart(mContext);
    }

    @JavascriptInterface
    public void userBehavior(String data) {
        Log.e(TAG, "userBehavior data: " + data);
        if (mContext instanceof MyWebViewActivity) {
            MyWebViewActivity activity = (MyWebViewActivity) mContext;
            activity.saveStudyTime();
        }
    }

    @JavascriptInterface
    public void pushToLogin(String data) {
        Log.e(TAG, "pushToLogin data: " + data);
        MainActivity.sMainActivity.showToast("超时或您已在其它终端登录，请重新登录");
        SharedPreferences sp = mContext.getSharedPreferences("AccountInfo", Context.MODE_PRIVATE);
        sp.edit().putString("loginData", "").apply();
        BaseApplication.getContext().setDataLogin(null);
        String[] array = LoginActivity.getAccountInfo(mContext);
        LoginActivity.actionStart(mContext, array[0], array[1]);
        ActivityManager.getInstance().finishActivities(ActivityManager.KEY_ALL_ACTIVITY);
    }

    @JavascriptInterface
    public void goBack(String data) {
        Log.e(TAG, "goBack data: " + data);
        if (mCallback != null)
            mCallback.goBack();
    }

    public interface Callback {
        void goBack();
    }
}
